
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;


namespace smasb2b
{
	public class VNDW0050 : BasePage
	{
		
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.WebControls.Label lblVNDW0060;
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME;
		protected System.Web.UI.WebControls.Label lblCLASS;
		protected System.Web.UI.WebControls.Label lblPAGEINDEX;
		protected System.Web.UI.WebControls.Label lblPAGE1;
		protected System.Web.UI.WebControls.Label lblTOTAL;
		protected System.Web.UI.WebControls.Label lblPAGE;
		protected System.Web.UI.WebControls.ImageButton btnPREV;
		protected System.Web.UI.WebControls.ImageButton BtnGo;
		protected System.Web.UI.WebControls.ImageButton BtnNext;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
		protected ddscControlLibrary.InputBoxCtrl PageIndex;
		protected ddscControlLibrary.InputBoxCtrl PageCount;
		protected ddscControlLibrary.InputBoxCtrl GotoPage;
		protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
		protected System.Web.UI.WebControls.Label lblVNDW0050;
		protected System.Web.UI.WebControls.Label lblRETURN_FLAG;
		protected System.Web.UI.WebControls.Label lblADMONISH_FLAG;
		protected System.Web.UI.WebControls.Label lblPURCHASE_DATE;
		protected ddscControlLibrary.DateCtrl txtPURCHASE_DATE2;
		protected System.Web.UI.WebControls.Label lblPURCHASE_NO1;
		protected ddscControlLibrary.InputBoxCtrl txtPURCHASE_NO;
		protected ddscControlLibrary.DateCtrl txtPURCHASE_DATE1;
		protected System.Web.UI.WebControls.Label lblPURCHASE_SEQ;
		protected System.Web.UI.WebControls.Label lblPURCHASE_NO;
		protected System.Web.UI.WebControls.Label lblRETURN_CONFIRM_FLAG;
		protected System.Web.UI.WebControls.Label lblCODE3;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		VENDOR LoginUser;
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			try
			{
				
				if (!Page.IsPostBack)
				{
					
					
				}
				else
				{
					if (IsReSubmit)
					{
						DataGrid_DataBind();
					}
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
            this.BtnNext.Click += BtnNext_Click;
            this.BtnGo.Click += BtnGo_Click;
            this.btnPREV.Click += btnPREV_Click;
            this.btnSearch.Click += btnSearch_Click;
		}
		
		private DataTable createErrorTable(string mError)
		{
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}
		
		public void DataGrid_DataBind()
		{
			//變數宣告
			LoginUser = GetLoginUser();
			DataSet ds = default(DataSet);
			dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
			string strCondition = "";
			int i = 0;
			int count = 2;
			string[] str_SQL_a = new string[count + 1];
			string[] str_SQL_b = new string[count + 1];
			
			try
			{
				//目前頁次指定Goto
				GotoPage.Text = Server.HtmlEncode(PageIndex.Text);
				
				//預設查詢語法
				StringBuilder SqlCommandText = new StringBuilder();
				SqlCommandText.Append("select * from ( SELECT DISTINCT  A.*,DECODE(B.ADMONISH_FLAG,\'\',\'NEW\') ADMONISH_FLAG from  ");
				SqlCommandText.Append(" (SELECT DISTINCT A.PURCHASE_NO ,DECODE(A.RETURN_CONFIRM_FLAG,\'Y\',\'*\') RETURN_CONFIRM_FLAG ,D.CLASS_NAME CLASS,A.VENDOR_NO,B.PURCHASE_SEQ,B.CODE CODE3,E.ENGLISH_SHORTNAME,A.PURCHASE_DATE FROM  ");
				SqlCommandText.Append(" PUR_PURCHASE_MASTER A,PUR_PURCHASE_DETAILS B,VND_VENDOR C,(select CLASS,(CLASS||\'-\'||CLASS_NAME)CLASS_NAME from BAS_MATERIAL_CLASS where CLASS_LEVEL=\'1\')  ");
				SqlCommandText.Append(" D,BAS_MATERIAL E WHERE A.PURCHASE_NO = B.PURCHASE_NO  AND A.VENDOR_NO=C.VENDOR_NO  AND B.CODE=E.CODE AND E.CLASS=D.CLASS  AND A.STATUS <>\'0\'   ");
                //SqlCommandText.Append("  AND C.VENDOR_NO=\'" + LoginUser.VENDOR_NO + "\' AND A.STATUS=\'9\' AND A.POST_B2B=\'Y\' ");
                SqlCommandText.Append("  AND C.VENDOR_NO=:VENDORNO AND A.STATUS=\'9\' AND A.POST_B2B=\'Y\' ");
                SqlCommandText.Append(" ) A , B2B_SHIPMENTS B where A.PURCHASE_NO=B.PURCHASE_NO(+) ) A");
				
				StringBuilder SqlCommandCount = new StringBuilder();
				SqlCommandCount.Append("Select Count(PURCHASE_NO) As row_total_count FROM(");
				SqlCommandCount.Append(" " + SqlCommandText.ToString() + " ");
				SqlCommandCount.Append(" ) ");
				
				//組合條件字串
				str_SQL_a[0] = txtPURCHASE_NO.Text.Replace(",", "\'\'");
                //str_SQL_b[0] = string.Format("PURCHASE_NO = \'{0}\' and ", str_SQL_a[0]);
                str_SQL_b[0] = "PURCHASE_NO = :SQLA0 and ";
				
				str_SQL_a[1] = txtPURCHASE_DATE1.Text;
				string PURCHASE_DATE2 = txtPURCHASE_DATE2.Text;
				if (str_SQL_a[1] == PURCHASE_DATE2)
				{
					str_SQL_b[1] = "PURCHASE_DATE = \'" + str_SQL_a[1] + "\' AND ";
                }
				else
				{
					if (str_SQL_a[1] == "")
					{
						str_SQL_a[1] = PURCHASE_DATE2;
						str_SQL_b[1] = "PURCHASE_DATE BETWEEN \'" + str_SQL_a[1] + "\' AND \'" + PURCHASE_DATE2 + "\' AND ";
                    }
					else
					{
						if (string.IsNullOrEmpty(PURCHASE_DATE2))
						{
							PURCHASE_DATE2 = str_SQL_a[1];
						}
                        str_SQL_b[1] = "PURCHASE_DATE BETWEEN \'" + str_SQL_a[1] + "\' AND \'" + PURCHASE_DATE2 + "\' AND ";
                    }
				}
				
				
				
				//查詢條件組合
				for (i = 0; i <= count - 1; i++)
				{
					if (str_SQL_a[i].Trim() != "")
					{
						strCondition = strCondition + str_SQL_b[i];
					}
				}
				
				if (!(String.IsNullOrEmpty(strCondition)))
				{
					strCondition = " WHERE " + strCondition.Substring(0, strCondition.Length - 5);
				}
				
				int intCount = 0;
				int size = PageSize; // Update by titan 由BasePage設定
                m_arrParam.Clear();
                m_arrParam.Add(dba.GetOracleParam(":VENDORNO", LoginUser.VENDOR_NO));
                if(str_SQL_a[0].Trim()!=""){
                     m_arrParam.Add(dba.GetOracleParam(":SQLA0", str_SQL_a[0]));
                }
				// 計算資料總筆數
				//DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString() + strCondition);
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString() + strCondition,m_arrParam);
				if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
				{
					intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
					PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
				}
				
				//Update By Titan 處理當總頁次小於目前頁次
				if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
				{
					// 總筆數(intCount)小於一頁，從第0筆開始顯示
					PageIndex.Text = Server.HtmlEncode(PageCount.Text);
					GotoPage.Text = Server.HtmlEncode(PageCount.Text);
				}
				// Update by titan 由BasePage設定
				// absolute position
				int position = ((int.Parse(PageIndex.Text)) - 1) * size;
				int maxrecord = position + size;
				
				
				//控制分頁按鈕
				ControlPageButton(DataGrid_foot);
				
				//執行資料庫語法
                //ds = dba.runExecuteQuery("PUR_PURCHASE_MASTER", SqlCommandText.ToString() + strCondition + " Order By A.PURCHASE_NO , A.PURCHASE_SEQ ", maxrecord, position);
                ds = dba.runExecuteQuery("PUR_PURCHASE_MASTER", SqlCommandText.ToString() + strCondition + " Order By A.PURCHASE_NO , A.PURCHASE_SEQ ", position, maxrecord,m_arrParam);
				
				if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
				{
					HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
					ArrayList ArrKey = new ArrayList();
					ArrKey.Add("PURCHASE_NO");
					ArrKey.Add("PURCHASE_SEQ");
					ArrKey.Add("CODE3");
					
					DataGrid_Bind_VNDW0050(DataGrid, ArrKey, ds.Tables["PUR_PURCHASE_MASTER"]);
					
				}
				else
				{
					ResourceManager rm = GetResourceManager();
					showException(rm.GetString("MSG069"));
				}
				
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			finally
			{
				ds.Dispose();
			}
		}
		
		private void btnSearch_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				PageIndex.Text = System.Convert.ToString(1);
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ResourceManager rm = GetResourceManager();
			try
			{

				PageIndex.Text = Server.HtmlEncode(GotoPage.Text);

				DataGrid_DataBind();
			}
			catch (Exception)
			{
				showException(rm.GetString("MSG057"));
			}
		}
		
		private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		public void DataGrid_Bind_VNDW0050(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
		{
			
			HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
			int i = 0;
			int j = 0;
			int k = 0;
			string Url = "";
			string className = "";
			ResourceManager rm = GetResourceManager();
			try
			{
				for (i = 0; i <= DataSource.Rows.Count - 1; i++)
				{
					HtmlTableRow htr = new HtmlTableRow();
					if (i % 2 == 0)
					{
						className = "evenRow";
					}
					else
					{
						className = "oddRow";
					}
					htr.Attributes.Add("class", className);
					DataGrid.Rows.Add(htr);
					for (j = 0; j <= tr.Cells.Count - 1; j++)
					{
						HtmlTableCell hd = new HtmlTableCell();
						if (DataSource.Rows[i]["RETURN_CONFIRM_FLAG"].ToString() == "*")
						{
							tr.Cells[1].Attributes["class"] = "link";
							tr.Cells[2].Attributes["class"] = "link";
						}
						else
						{
							
							tr.Cells[1].Attributes["class"] = "else";
							tr.Cells[2].Attributes["class"] = "else";
						}
						switch (tr.Cells[j].Attributes["class"].ToLower())
						{
							case "link":
								Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
								for (k = 0; k <= ArrKey.Count - 1; k++)
								{
									Url += "&" + ArrKey[k] + "=" + DataSource.Rows[i][ArrKey[k].ToString()].ToString();
								}
								
								hd.Width = tr.Cells[j].Width;
								hd.InnerHtml = string.Format("<a href=\"javascript:window.showModalDialog(\'{0}\',this,\'{1}\', true)\">{2}</a>", Url, "dialogWidth=730px;dialogHeight=440px;scrollbars=yes;center=yes;border=thin;help=yes;status=yes;resizable=yes;", rm.GetString(tr.Cells[j].ID));
								break;
							case "tdtitle":
								hd.Width = tr.Cells[j].Width;
								hd.Attributes.Add("class", "detail");
								hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
								break;
							default:
								hd.Width = tr.Cells[j].Width;
								hd.InnerText = rm.GetString(tr.Cells[j].ID);
								break;
						}
						DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);
						
					}
				}
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			
			
		}
		
	}
	
}
